package com.seal.dao.impl;

import java.util.List;

import org.hibernate.Query;
import org.springframework.stereotype.Repository;

import com.seal.comm.utils.Page;
import com.seal.dao.BaseDaoImpl;
import com.seal.dao.ITemplateDao;
import com.seal.model.Template;

@Repository("templateDaoImpl")
public class TemplateDaoImpl extends BaseDaoImpl implements ITemplateDao {

	public void addTemplate(Template Template) throws Exception {
		this.save(Template);
	}

	public void updateTemplate(Template Template) throws Exception {
		this.update(Template);
	}

	public Template getTemplateById(String id) throws Exception {
		String hql = new StringBuffer("from Template t where t.id = ? ")
				.toString();
		return (Template) this.getSession().createQuery(hql).setString(0, id)
				.uniqueResult();
	}

	public List<Template> getTemplate(String busitype, String lang) throws Exception {
		String hql = "from Template t where t.busiType='" + busitype + "'";
		if (lang != null)
			hql += " and lang='" + lang + "'";
		return (List<Template>) this.getSession().createQuery(hql).list();
	}

	public List<Template> getAllTemplate(String lang, Page page)
			throws Exception {
		StringBuffer hql = new StringBuffer("from Template t where 1=1");
		if (null != lang && !"".equals(lang.trim())) {
			hql.append(" and t.lang='" + lang+"'");
		}

		Query query = this.getSession().createQuery(hql.toString());
		if (null != page) {
			query.setFirstResult(page.getFirstResult());
			query.setMaxResults(page.getPageSize());
			String countSql = "select count(t) " + hql.toString();

			Query queryCount = this.getSession().createQuery(countSql);
			page.setTotalNumber(Integer.parseInt(queryCount.uniqueResult()
					.toString()));
		}

		return query.list();
	}

}
